#include<iostream>
#include<string>
#include<unistd.h>
#include<sys/types.h>

using namespace std;

class Handler {
public:
    void operator()(int sock, string ip, int port) {
            char buf[1024];
            while (true) {
                ssize_t size = read(sock, buf, sizeof(buf) - 1);
                if (size > 0) {
                    buf[size] = 0;  
                    cout << ip << "&" << port << ":" << buf << endl;
                    write(sock, buf, size);
                }
                else if(size == 0) {
                    cout << ip << "&" << port << " close!" << endl;
                    break;
                }
                else {
                    perror("read");
                    break;
                }
            }
            close(sock);
            cout << "Service done!" << endl;
    }
};
class Task {
public:
    Task(){}
    Task(int sock, string ip, int port) : _sock(sock), _ip(ip), _port(port) {
    }

    void Run() {
        _handler(_sock, _ip, _port);
    }
private:
    int _sock;
    string _ip;
    int _port;
    Handler _handler;
};